<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;

class ModelFields extends Common
{
    public function index()
    {
        //获取表的前缀
        $prefix= config("database.prefix");
        $modelName=$prefix.'model';
        //连表查询所需要的表名及字段名称
        $fieldRes=db('model_fields')->field('f.*,m.model_name')->alias('f')->join("$modelName m",'f.model_id=m.id')->order('f.id desc')->paginate(6);
        $this->assign('fieldRes',$fieldRes);
        return view();
    }
    public function add(){

        if (request()->isPost()){
            $data=input('post.');
            $tableName=db('model')->field('table_name')->find($data['model_id']);
            $tableName=config("database.prefix").$tableName['table_name'];
            //过滤中文逗号
            if ( $data['field_values']){
                $data['field_values']=str_replace('，',',',$data['field_values']);
            }
            //验证数据
            $validate=validate('ModelFields');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $add=db('model_fields')->insert($data);
            if ($add){
                switch ($data['field_type']){
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 6:$fieldType='varchar(150) not null default "" ';
                        break;
                    case 5:$fieldType='varchar(600) not null default "" ';
                        break;
                    case 7:$fieldType='float not null default "0.0" ';
                        break;
                    case 8:$fieldType='int not null default "0" ';
                        break;
                    case 9:$fieldType='longtext ';
                        break;
                    default:$fieldType='varchar(150) not null default "" ';
                        break;
                }
                $sql="alter table {$tableName} add {$data['field_ename']} {$fieldType}";
                Db::execute($sql);
                $this->success('添加字段成功!','index');
            }else{
                $this->error('添加字段失败!');
            }
            return;
        }
        $modelRes=db('model')->field('id,model_name')->select();
        $this->assign([
            'modelRes'=>$modelRes,
        ]);
        return view();
    }

    public function edit(){

        if (request()->isPost()){
            $data=input('post.');
            //获取表的前缀
            $prefix= config("database.prefix");
            $modelName=$prefix.'model';
            //连表查询所需要的表名及字段名称
            $fieldInfo= db('model_fields')->field('a.field_ename,b.table_name')->alias('a')->join("$modelName b", 'a.model_id=b.id')->find($data['id']);
            //修改字段的表名
            $tableName=$prefix.$fieldInfo['table_name'];
            //修改字段的名称
            $oldFieldName=$fieldInfo['field_ename'];
            //过滤中文逗号
            if ( $data['field_values']){
                $data['field_values']=str_replace('，',',',$data['field_values']);
            }
            //验证数据
            $validate=validate('ModelFields');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $save=db('model_fields')->update($data);
            //修改数据表字段
            switch ($data['field_type']){
                case 1:
                case 2:
                case 3:
                case 4:
                case 6:$fieldType='varchar(150) not null default "" ';
                    break;
                case 5:$fieldType='varchar(600) not null default "" ';
                    break;
                case 7:$fieldType='float not null default "0.0" ';
                    break;
                case 8:$fieldType='int not null default "0" ';
                    break;
                case 9:$fieldType='longtext ';
                    break;
                default:$fieldType='varchar(150) not null default "" ';
                    break;
            }
            $sql="alter table {$tableName} change {$oldFieldName} {$data['field_ename']} {$fieldType}";
            Db::execute($sql);
            if ($save!==false){
                $this->success('修改字段成功!','index');
            }else{
                $this->error('修改字段失败!');
            }
            return;
        }
        $modelRes=db('model')->field('id,model_name')->select();
        $modelFields=db('model_fields')->find(input('id'));
        $this->assign(array(
            'modelRes'=>$modelRes,
            'modelFields'=>$modelFields
        ));
        return view();
    }
    //删除字段
    public function ajaxDel(){
        $modelFieldsId=input('id');
        //获取表的前缀
       $prefix= config("database.prefix");
        $modelName=$prefix.'model';
        //连表查询所需要的表名及字段名称
       $fieldInfo= db('model_fields')->field('a.field_ename,b.table_name')->alias('a')->join("$modelName b", 'a.model_id=b.id')->find($modelFieldsId);
        //删除字段的表名
        $tableName=$prefix.$fieldInfo['table_name'];
        //删除字段的名称
        $fieldName=$fieldInfo['field_ename'];
        $del=db('modelFields')->delete($modelFieldsId);
        $sql="alter table {$tableName} drop column {$fieldName}";
        Db::execute($sql);
        if ($del){
            echo 1;
        }else{
            echo 0;
        }
    }

}
